/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.worldjb.vk;

import java.awt.Toolkit;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.swing.ImageIcon;
import ru.worldjb.vk.util.MainProps;

/**
 * Форма с данными профиля
 * @author alex_xpert
 */
public class ProfileFrame extends javax.swing.JFrame {
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
	
    private String at = MainProps.getProperty("access_token");

    public ProfileFrame() {
        initComponents();
        // Выполняем запрос к API ВКонтакте посредством HttpURLConnection
        // и отображаем информацию на форме
        try {
            HttpURLConnection con = (HttpURLConnection) new URL("https://api.vk.com/method/users.get"
                                           + "?fields=sex,bdate,city,country,photo_200,domain,status"
                                           + "&access_token=" + at).openConnection();
            con.connect();
            InputStreamReader reader = new InputStreamReader(con.getInputStream());
            StringBuffer sb = new StringBuffer();
            int b;
            while((b = reader.read()) != -1) sb.append((char) b);
            reader.close();
            con.disconnect();
            String res = sb.toString();
            setIconImage(Toolkit.getDefaultToolkit().getImage("F:/Mezhdunarodny_logotip_VK.png"));
            setTitle(res.split("\"domain\":\"")[1].split("\",")[0]);
            jLabel1.setIcon(new ImageIcon(Toolkit.getDefaultToolkit().getImage(new URL(res.split("\"photo_200\":\"")[1].split("\",")[0].replace("\\", "")))));
            String first_name = res.split("\"first_name\":\"")[1].split("\",")[0];
            String last_name = res.split("\"last_name\":\"")[1].split("\",")[0];
            String status = res.split("\"status\":\"")[1].split("\"")[0];
            jLabel2.setText("<html><center><b>" + first_name + " " + last_name + "</b><br>Статус: " + status + "</center></html>");
        } catch(Exception ex) {
            ex.printStackTrace();
        }
    }

    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 190, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 100, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }

}
